/* This file aims to produce summary statistics and run regressions (including reduced-form analysis and estimating policy functions   */
clear all
set more off
cd ""    //target directory


clear
insheet using choice.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newchoice`i'
}
reshape long newchoice,i(ahaid year) j(vid)
tempfile chs
save `chs'

insheet using vlag.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvlag`i'
}
reshape long newvlag,i(ahaid year) j(vid)
tempfile vlag
save `vlag'


insheet using exov1mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mkt`i'
}
reshape long newv1mkt,i(ahaid year) j(vid)
tempfile v1mkt
save `v1mkt'



insheet using exov2mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mkt`i'
}
reshape long newv2mkt,i(ahaid year) j(vid)
tempfile v2mkt
save `v2mkt'


insheet using exovmksh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktsh`i'
}
reshape long newvmktsh,i(ahaid year) j(vid)
tempfile vmktsh
save `vmktsh'


insheet using v1sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1sys`i'
}
reshape long newv1sys,i(ahaid year) j(vid)
tempfile v1sys
save `v1sys'


insheet using v2sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2sys`i'
}
reshape long newv2sys,i(ahaid year) j(vid)
tempfile v2sys
save `v2sys'


insheet using vsysh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvsysh`i'
}
reshape long newvsysh,i(ahaid year) j(vid)
tempfile vsysh
save `vsysh'



//IV inputs  ### UPDATED on 07/24/2019: Philipp mentioned using an alternative IV, which is the vendor share within the multi-region chain; input for IV is altiv_dom/msh_tmp.csv
clear
insheet using "./prepare/iv_dom_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivdom`i'
}
reshape long ivdom,i(hsayr_id) j(vid)
tempfile ivdom
save `ivdom'


insheet using "./prepare/iv_msh_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivmsh`i'
}
reshape long ivmsh,i(hsayr_id) j(vid)
tempfile ivmsh
save `ivmsh'



insheet using exovtipmsh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvtipmsh`i'
}
reshape long newvtipmsh,i(ahaid year) j(vid)
merge 1:1 ahaid year vid using `chs'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vlag'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktsh'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vsysh'
keep if _merge==3
drop _merge
tempfile vtipmsh
save `vtipmsh'


clear
use "./prepare/allbasic.dta",clear
merge 1:m ahaid year using `vtipmsh'
keep if _merge==3
drop _merge


sort sysid year
egen sysyr_id=group(sysid year)
sort hsa year
egen hsayr_id=group(hsa year)



********** Merge with IVs
merge m:1 hsayr_id vid using `ivmsh'
*keep if _merge==3
drop _merge

merge m:1 hsayr_id vid using `ivdom'
*keep if _merge==3
drop _merge




********** Merge with market characterisitcs (demographics and market-category effects)
******  Demographics: population and income per capita
preserve
clear
use "./prepare/demographic4mkt"
duplicates drop hsa year,force
keep hsa year totpop totpop65 avginc
rename totpop65 totpop65_ori
gen perc65=totpop65_ori/totpop
replace totpop=log(totpop)
gen totpop65=log(totpop65_ori)
replace avginc=log(avginc)
tempfile pop
save `pop'
restore
******


****** Create HHI at the market level using total admissions: admtot
preserve
keep if vid==13
keep id year admtot hsa
bys hsa year:egen totadm_mkt=total(admtot)
gen admsh=admtot/totadm_mkt
bys hsa year:egen hhi=total(admsh^2)
duplicates drop hsa year,force
keep hsa year hhi
tempfile hhi
save `hhi'
restore
******


****** Market-category effects (using adoption ratio as DV)
preserve

clear
input hsa mktfe
1 0
end
tempfile firsthsa
save `firsthsa'


clear
import delim using "./prepare/mktFEs_comb1.csv",delimiters(",") varnames(1)
rename (v1 comb) (hsa mktfe)
drop if strpos(hsa,"_Ihsa_")==0
replace hsa=subinstr(hsa,"_Ihsa_","",1)
destring,replace force
append using `firsthsa',force

gen below25cat=0
gen below50cat=0
gen below75cat=0
gen below100cat=0

sum mktfe,detail

replace below25cat=1 if mktfe<=r(p25)
replace below50cat=1 if mktfe<=r(p50)&mktfe>r(p25)
replace below75cat=1 if mktfe<=r(p75)&mktfe>r(p50)
replace below100cat=1 if mktfe>r(p75)

tempfile mktfe_comb
save `mktfe_comb'

restore
******


****** Merge files

merge m:1 hsa using `mktfe_comb'
keep if _merge==3
drop _merge


merge m:1 hsa year using `pop'
keep if _merge==3
drop _merge


merge m:1 hsa year using `hhi'
keep if _merge==3
drop _merge





********** Create bed size quartiles
gen bdrng=0
sum bdtot if sysid==0,detail  

replace bdrng=1 if bdtot<=r(p25)
replace bdrng=2 if bdtot>r(p25) & bdtot<=r(p50)
replace bdrng=3 if bdtot>r(p50) & bdtot<=r(p75)
replace bdrng=4 if bdtot>r(p75)
tab bdrng,gen(bdqntl)


foreach i in 1 2 3 4 {
gen v1bd`i'=newv1mkt*bdqntl`i'
gen vmshbd`i'=newvmktsh*bdqntl`i'
gen vlagbd`i'=newvlag*bdqntl`i'
}


forvalues i=2/13 {
gen bed`i'=bdtot*(vid==`i')
replace bed`i'=bed`i'/100
}


gen adopt=(adj_vnd>1)
bys hsa year vid:gen nhosp=_N






**### UPDATED on 12/16/2020: Generate minoraff indicators (at the HSA level)
*### UPDATED on 12/24/2020: only keep the IVsample
gen want=(ivmsh~=.)


**!!Also include affiliated hospitals that belong to the system included in the original minoraff!!
bys sysid year vid:egen withwant=max(want==1)
replace withwant=0 if sysid==0
*### UPDATED on 12/17/2020: only keep if want==1 because the base for bootstrap depends on HSAs (markets)
keep if want==1
*### UPDATED on 12/17/2020: NEED to sort hsa year to generate NEW hsa ID, because in the minoraff sample, HSA is not in a balanced panel (Some hsa missing in certain years)
sort hsa year vid
egen newhsa=group(hsa year)



***&&&*** generate the set of HSAs for bootstrapping (50 bootstraps); samples generated in Matlab
preserve
keep if choice==1
duplicates drop newhsa,force
keep newhsa nhosp
sort newhsa
export delim newhsa nhosp using "./prepare/list_mkt_IVsample.csv",delim(",") novar replace 
restore
***&&&***




***$$$*** Inputs: MUST include HSA, because bootstrapping is blocked by HSA; use "ahaid"!!!
** Create matrix Y --- N x 1 vector of integers 1 through J indicating which alternative was chosen
preserve
keep if choice==1
sort ahaid year
export delim ahaid year newhsa adj_vnd prechs sysid using "./prepare/matY.csv",delim(",") novar replace 
restore


** Create matrix X --- N x K1 matrix of individual-specific covariates
preserve
keep if choice==1
gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

sort ahaid year
export delim ahaid year newhsa bdtot prechs nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/matX.csv",delim(",") novar replace 
restore


** Create matrix Z --- N x K2 x J array of covariates that are alternative-specific
preserve
gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

sort ahaid year vid
export delim ahaid year vid newhsa vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd1 vlagbd2 vlagbd3 vlagbd4 newvlag newvsysh below50cat below75cat below100cat prechs sysid using "./prepare/matZ.csv",delim(",") novar replace 
restore





